package com.yjck.ijpay.utils.zzb;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import org.springframework.core.io.ClassPathResource;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 
 ****************************************************************************
 * Copyright (C) 2017 河南坚磐电子科技有限公司 All Rights Reserved
 ****************************************************************************
 * 【编写时间】 2018年7月5日 【编 写 人】 韩川 【功能概要】 支付工具类
 ****************************************************************************
 *
 */

public class PayUtils {

	/**
	 * 读取支付宝配置文件
	 * 
	 * @return
	 */
	public static Map<String, String> aliPayProperties() {
		Map<String, String> properties = new HashMap<String, String>();
		Properties p = new Properties();
		try {
			ClassPathResource cpr = new ClassPathResource("alibabapay.properties");
			InputStream inputStream = cpr.getInputStream();
			p.load(inputStream);
			String payAPPID = p.getProperty("payAPPID");
			String payWAY = p.getProperty("payWAY");
			String publickey = p.getProperty("publickey");
			String aliPublicKey = p.getProperty("aliPublicKey");
			String privateKey = p.getProperty("privateKey");
			String format = p.getProperty("format");
			String charset = p.getProperty("charset");
			String sign_type = p.getProperty("sign_type");
			String notice_URL = p.getProperty("notice_URL");

			properties.put("payAPPID", payAPPID);
			properties.put("payWAY", payWAY);
			properties.put("publickey", publickey);
			properties.put("aliPublicKey", aliPublicKey);
			properties.put("privateKey", privateKey);
			properties.put("format", format);
			properties.put("charset", charset);
			properties.put("sign_type", sign_type);
			properties.put("notice_URL", notice_URL);

			if (inputStream != null) {
				inputStream.close();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return properties;
	}

	public static void main(String[] args) {
		Map<String, String> aliPayProperties = aliPayProperties();
		String payAPPID = aliPayProperties.get("payAPPID");
		System.out.println(payAPPID);
	}

	/**
	 * 获取支付宝Client对象
	 * 
	 * @return
	 */
	public static AlipayClient getAlipayClient() {
		Map<String, String> aliPayProperties = aliPayProperties();
		String payAPPID = aliPayProperties.get("payAPPID");
		String payWAY = aliPayProperties.get("payWAY");
		String aliPublicKey = aliPayProperties.get("aliPublicKey");
		String privateKey = aliPayProperties.get("privateKey");
		String format = aliPayProperties.get("format");
		String charset = aliPayProperties.get("charset");
		String sign_type = aliPayProperties.get("sign_type");

		AlipayClient alipayClient = new DefaultAlipayClient(payWAY, payAPPID, privateKey, format, charset, aliPublicKey,
				sign_type);
		return alipayClient;
	}
	
	/**
	 * 获取支付宝回调URL前缀
	 * @return
	 */
	public static String getNoticeUrl(){
		Map<String, String> aliPayProperties = aliPayProperties();
		return aliPayProperties.get("notice_URL");
	}

}
